<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * 打印输出
 * @param array $obj
 * @param int $exit
 */
function p($obj = array(),$exit = 0,$format=1){
    if($format){
        echo "<pre>";
        print_r($obj);
        echo "</pre>";
    } else {
        print_r($obj);
    }
    $exit && exit;
}
function base64EncodeImage ($image_file) {
    $base64_image = '';
    $image_info = getimagesize($image_file);

    $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
   // $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
    $base64_image = chunk_split(base64_encode($image_data));
    return $base64_image;
}
function curl_request ( $url , $post = '' , $cookie = '' ,  $returnCookie = 0 ) {
    $curl  =  curl_init ( ) ;
    curl_setopt ( $curl , CURLOPT_URL ,  $url ) ;
    curl_setopt ( $curl , CURLOPT_USERAGENT ,  'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)' ) ;
    curl_setopt ( $curl , CURLOPT_FOLLOWLOCATION ,  1 ) ;
    curl_setopt ( $curl , CURLOPT_AUTOREFERER ,  1 ) ;
    curl_setopt ( $curl , CURLOPT_REFERER ,  "http://XXX" ) ;
    if ( $post )  {
        curl_setopt ( $curl , CURLOPT_POST ,  1 ) ;
        curl_setopt ( $curl , CURLOPT_POSTFIELDS ,  http_build_query ( $post ) ) ;
    }
    if ( $cookie )  {
        curl_setopt ( $curl , CURLOPT_COOKIE ,  $cookie ) ;
    }
    curl_setopt ( $curl , CURLOPT_HEADER ,  $returnCookie ) ;
    curl_setopt ( $curl , CURLOPT_TIMEOUT ,  10 ) ;
    curl_setopt ( $curl , CURLOPT_RETURNTRANSFER ,  1 ) ;
    $data  =  curl_exec ( $curl ) ;
    if  ( curl_errno ( $curl ) )  {
        return  curl_error ( $curl ) ;
    }
    curl_close ( $curl ) ;
    if ( $returnCookie ) {
        list ( $header ,  $body )  =  explode ( "\r\n\r\n" ,  $data ,  2 ) ;
        preg_match_all ( "/Set\-Cookie:([^;]*);/" ,  $header ,  $matches ) ;
        $info [ 'cookie' ]   =  substr ( $matches [ 1 ] [ 0 ] ,  1 ) ;
        $info [ 'content' ]  =  $body ;
        return  $info ;
    } else {
        return  $data ;
    }
}

function getMillisecond() {
    list($s1, $s2) = explode(' ', microtime());
    return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
}


function arrayFormats( $input = [] ,$changes = [] ){
    if( is_array( $input ) ){
        foreach( $input as $k => $v ){
            $input[ $k ] = arrayFormats( $v,$changes );
        }
    } else {
        if( isset($changes[$input]) && !empty($changes[$input]) ){
            return $changes[$input];
        }
    }
    return $input;
}
